package com.voole.service.impl;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.voole.dao.BaseDao;
import com.voole.dao.SysActionDao;
import com.voole.dao.SysAuthorityDao;
import com.voole.service.ISecurityService;

@Service
public class SecurityServiceImpl implements ISecurityService {

	@Autowired
	private SysActionDao sysActionDao;

	/**
	 * 根据 请求地址(request.getServletPath)、用户权限 查询 页面按钮
	 */
	public String getSecurityButtonForPage(HttpServletRequest request) throws Exception {
		List<Integer> securitySysUserActionIdList = (List<Integer>) request.getSession().getAttribute("SECURITY_SYS_USER_ACTION_IDS");
		Integer[] userActionIdIntArray = securitySysUserActionIdList.toArray(new Integer[]{});
		String[]  userActionIdStrArray = changeIntegerArrayToStringArray(userActionIdIntArray);
		String userActionIds = String.join(",", userActionIdStrArray);
		List<String> buttonCodeList = sysActionDao.getColumnCodeForSecurityButton(request.getServletPath(), userActionIds);
		
		StringBuffer sb = new StringBuffer();
		for(String buttonCode : buttonCodeList){
			sb.append("&nbsp;");
			sb.append(buttonCode);
		}
		
		return sb.toString();
	}
	
	/**
     * 将 Integer数组 转换成 String数组
     *  (Integer[] -> String[])
     */
    private String[] changeIntegerArrayToStringArray(Integer[] integerArray){
    	String[] stringArray = new String[integerArray.length];
    	for(int i=0; i<integerArray.length; i++){
    		if(integerArray[i]==null){
    			continue;
    		}
    		stringArray[i] = integerArray[i].toString();
    	}
    	return stringArray;
    }
	
	
}
